package problem;

import java.util.Iterator;

import agents.Agent;
import agents.AgentImpl;

import utils.Dijkstra;
import env.City;
import env.DistList;
import env.World;

public class Heuristic {

	public static int getHeuristic(World world, City goal) {
		int res=0;
		Iterator<City> it = world.getCities().iterator();
		while (it.hasNext()) {
			City city = (City) it.next();
			int chemsInCity = city.getChems();
			if (chemsInCity>0) {
				DistList distList = Dijkstra.runDijkstra(world, city, false);
				int distance = distList.getDistance(goal);
				if(distance<Integer.MAX_VALUE)
					res=res + distance*chemsInCity;
			}
		}
		return res*2;
	}
	public static int[] cooperativeHeuristic(World world, AgentImpl friend ,  AgentImpl foe)
	{
		int res=0;
		int hisRes=0;
		Iterator<City> it = world.getCities().iterator();
		while (it.hasNext()) {
			City city = (City) it.next();
			int chemsInCity = city.getChems();
			if (chemsInCity>0) {
				DistList distList = Dijkstra.runDijkstra(world, city, false);
				int mydistance = distList.getClosestDistance(friend.getGoals());
				if(mydistance<Integer.MAX_VALUE)
					res=res + (mydistance - friend.R)*chemsInCity;
				int hisdistance = distList.getClosestDistance(foe.getGoals());
					hisRes=hisRes + (hisdistance - foe.R)*chemsInCity;
			}
		}
		return new int[] {res*2,hisRes*2};
	}
	public static int zeroSumHeuristic(World world, AgentImpl friend, AgentImpl foe)
	{
		
		int res=0;
		Iterator<City> it = world.getCities().iterator();
		while (it.hasNext()) {
			City city = (City) it.next();
			int chemsInCity = city.getChems();
			if (chemsInCity>0) {
				DistList distList = Dijkstra.runDijkstra(world, city, false);
				int mydistance = distList.getClosestDistance(friend.getGoals());
				int hisdistance = distList.getClosestDistance(foe.getGoals());
				if(mydistance<Integer.MAX_VALUE)
					res=res + (mydistance - friend.R)*chemsInCity;
				if(hisdistance<Integer.MAX_VALUE)
					res=res - (hisdistance + foe.R)*chemsInCity;
			}
		}
		//System.out.println("heuristic:"+res*2);
		return res*2;
	}
	public static int fullCooperativeHeuristic(World world, AgentImpl friend, AgentImpl foe)
	{
		int res=0;
		Iterator<City> it = world.getCities().iterator();
		while (it.hasNext()) {
			City city = (City) it.next();
			int chemsInCity = city.getChems();
			if (chemsInCity>0) {
				DistList distList = Dijkstra.runDijkstra(world, city, false);
				int mydistance = distList.getClosestDistance(friend.getGoals());
				int hisdistance = distList.getClosestDistance(foe.getGoals());
				if(mydistance<Integer.MAX_VALUE||hisdistance<Integer.MAX_VALUE)
					res=res + Math.min(mydistance-friend.R,hisdistance-foe.R)*chemsInCity;
				
			}
		}
//		System.out.println(res);
		return res;
	}
}
